package cn.yitun.chat.yitunservermanagecore.general.manage.common.service.impl;

import cn.yitun.chat.yitunservercommonlib.crud.yitun.CrudServiceImpl;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.common.DictDataDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.common.DictData;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.system.user.UserInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.common.DictDataVo;
import cn.yitun.chat.yitunservercommonlib.util.cache.RedisManager;
import cn.yitun.chat.yitunserverimbase.dao.common.DictDataDao;
import cn.yitun.chat.yitunservermanagecore.general.manage.common.service.DictDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class DictDataServiceImpl
		extends CrudServiceImpl<DictDataDao,DictDataDto, DictData, DictDataVo>
		implements DictDataService {

	@Resource
	private RedisManager redisManager;


	@Override
	public List<DictDataVo> list(String type, String key) {
		Map<String , Object> params = new HashMap<>();
		params.put(type, key);
		return listVO(params);
	}
	@Transactional
	@Override
	public int save(DictDataDto dictData){
		delCache(DictData.sail_dictkey + dictData.getType());
		return dao.save(dictData);
	}

	private void delCache(String  key) {
		redisManager.del(key);
	}

	@Transactional
	@Override
	public int update(DictDataDto dictData){
		delCache(DictData.sail_dictkey + dictData.getType());
		return dao.update(dictData);
	}
	@Transactional
	@Override
	public int remove(Long id){
		DictData dictData = get(id);
		if(dictData != null ){
			delCache(DictData.sail_dictkey + dictData.getType());
			return dao.remove(id);
		}
		return 0 ;
	}
	@Transactional
	@Override
	public int batchRemove(Long[] ids){
		if(ids.length > 0){
			DictData dictData = null;
			for(int i = 0 ; i < ids.length ; i ++ ){
				dictData = get(ids[i]);
				if(dictData != null ){
					delCache(DictData.sail_dictkey + dictData.getType());
				}
			}
			return dao.batchRemove(ids);
		}
		return 0 ;
	}
	@Transactional
	@Override
	public int absDel(Long id){
		DictData dictData = get(id);
		if(dictData != null ){
			delCache(DictData.sail_dictkey + dictData.getType());
		}
		return dao.absDel(id);
	}
	@Transactional
	@Override
	public int batchAbsDel(Long[] ids){
		if(ids.length > 0){
			DictData dictData = null;
			for(int i = 0 ; i < ids.length ; i ++ ){
				dictData = get(ids[i]);
				if(dictData != null ){
					delCache(DictData.sail_dictkey + dictData.getType());
				}
			}
			return dao.batchAbsDel(ids);
		}
		return 0 ;
	}



	@Override
	public List<DictDataVo> findListByType(String type) {
		if(redisManager.exists(DictData.sail_dictkey + type)){
			try {
				Object object = redisManager.getObject(DictData.sail_dictkey + type);
				return (List<DictDataVo>)object;
			}catch (Exception e){
				log.error(e.getMessage());
			}
		}
		Map<String, Object> param = new HashMap<>(16);
		param.put("type", type);
		List<DictDataVo> dictDataList = listVO(param);
		if(dictDataList != null && dictDataList.size() > 0 )
			redisManager.setObject(DictData.sail_dictkey + type , dictDataList , 0);
		return dictDataList;
	}



	/**
	 * 根据字典类型和字典键值查询字典数据信息
	 * @param dictType  字典类型
	 * @param dictValue 字典键值
	 * @return 字典标签
	 */
	@Override
	public String selectDictLabel(String dictType, String dictValue) {
		return dao.selectDictLabel(dictType, dictValue);
	}

	@Override
	public String getName(String type, String value) {
		Map<String, Object> param = new HashMap<String, Object>(16);
		param.put("type", type);
		param.put("value", value);
		String rString = list(param).get(0).getName();
		return rString;
	}

	@Override
	public List<DictDataVo> getHobbyList(UserInfo userDO) {
		Map<String, Object> param = new HashMap<>(16);
		param.put("type", "hobby");
		List<DictDataVo> hobbyList = listVO(param);

		return hobbyList;
	}

	@Override
	public List<DictDataVo> getSexList() {
		Map<String, Object> param = new HashMap<>(16);
		param.put("type", "sex");
		return listVO(param);
	}

}
